@echo off
set /p operational_mode=Enter: Cancel[0], Install[1], Update[2]: 
if -%operational_mode%-==-0- set operational_mode_exit=1 & echo Canceling & goto :eof
if -%operational_mode%-==-1- set operational_mode_install=1 & echo Starting Install & goto:run
if -%operational_mode%-==-2- set operational_mode_update=1 & echo Starting Update & goto:run
pause
goto :eof

:run
set steamcmd=C:\steamcmd\steamcmd.exe
set appid_client=602960
set appid_server=1026340
set install_dir_server=C:\Game\Barotrauma\Source
set install_dir_lfb=C:\Game\Barotrauma\Lua For Barotrauma
set lua_id=2559634234
set lua_dir=%install_dir_server%\steamapps\workshop\content\%appid_client%\%lua_id%

setlocal EnableDelayedExpansion
for %%i in (
    "%install_dir_server%"
    "%install_dir_lfb%"
) do (
    set dir=%%~di%%~pi
    if not exist !dir! (
        mkdir !dir!
    )
)
endlocal

%steamcmd% ^
+force_install_dir %install_dir_server% ^
+login anonymous ^
+app_update %appid_server% -validate ^
+workshop_download_item %appid_client% %lua_id% ^
+quit

call :copy_server_files .\ root.ignoresfiles
call :copy_server_files Data\ data.ignoresfiles
call :copy_server_files Content\
call :copy_server_files LocalMods\

call :copy_luacs_files Lua\
call :copy_luacs_files Publicized\
call :copy_luacs_files DedicatedServer.deps.json
call :copy_luacs_files DedicatedServer.dll
call :copy_luacs_files DedicatedServer.pdb
call :copy_luacs_files BarotraumaCore.dll
call :copy_luacs_files BarotraumaCore.pdb
call :copy_luacs_files 0Harmony.dll
call :copy_luacs_files Sigil.dll
call :copy_luacs_files MoonSharp.Interpreter.dll
call :copy_luacs_files MoonSharp.VsCodeDebugger.dll
call :copy_luacs_files MonoMod.Common.dll
call :copy_luacs_files Mono.Cecil.dll
call :copy_luacs_files Mono.Cecil.Mdb.dll
call :copy_luacs_files Mono.Cecil.Pdb.dll
call :copy_luacs_files Mono.Cecil.Rocks.dll
call :copy_luacs_files Microsoft.CodeAnalysis.CSharp.Scripting.dll
call :copy_luacs_files Microsoft.CodeAnalysis.CSharp.dll
call :copy_luacs_files Microsoft.CodeAnalysis.dll
call :copy_luacs_files Microsoft.CodeAnalysis.Scripting.dll
call :copy_luacs_files System.Collections.Immutable.dll
call :copy_luacs_files System.Reflection.Metadata.dll
call :copy_luacs_files System.Runtime.CompilerServices.Unsafe.dll
call :copy_luacs_files mscordaccore_amd64*.dll

goto :eof

:copy_server_files
setlocal EnableDelayedExpansion
set src=%install_dir_server%\%1
set dest=%install_dir_lfb%\%1
if !src:~-1!==\ (
    set src=!src:~0,-1!
    set dest=!dest:~0,-1!
    if %1==.\ (
        set options=/i /y
    ) else (
        set options=/i /e /y
    )
) else (
    set options=/y
)
set arguments=!options! "!src!" "!dest!"
if defined operational_mode_update (
    set arguments=!arguments! /exclude:%2
)
xcopy !arguments!
endlocal
exit /b

:copy_luacs_files
setlocal EnableDelayedExpansion
set src=%lua_dir%\Binary\%1
set dest=%install_dir_lfb%\%1
if !src:~-1!==\ (
    set src=!src:~0,-1!
    set options=/i /e /y
) else (
    set options=/y
)
set arguments=!options! "!src!" "!dest!"
echo Do: xcopy !arguments!
xcopy !arguments!
endlocal
exit /b
